import Vue from 'vue'
import Router from 'vue-router'
// import Layout from '../components/layout/Layout'
// import User from './user/user'
// import Admin from './admin/admin'
// import store from '../store'
// import Error from './error/error'
// import { Alert } from '../base/index'
Vue.use(Router)

// export const redirectRouter = [{
//   path: '/',
//   redirect: {
//     name: 'user'
//   }
// }]
export const constantRouterMap = [
  {
    path: '/',
    redirect: {
      name: 'user'
    },
    hidden: true
  },
  {
    path: '/user',
    hidden: true,
    component: resolve => require(['@/components/user/index'], resolve),
    name: 'user',
    meta: { title: '用户页面' },
    redirect: {
      name: 'login'
    },
    children: [
      {
        path: 'login',
        hidden: true,
        component: resolve => require(['@/components/user/login/login'], resolve),
        name: 'login',
        meta: { title: '用户登陆' }
      }
      // {
      //   path: 'signUp',
      //   hidden: true,
      //   component: resolve => require(['@/components/user/login/signUp'], resolve),
      //   name: 'signUp',
      //   meta: { title: '用户注册' }
      // }
    ]
  },
  {
    path: '/admin',
    name: 'admin',
    meta: {
      title: '管理页面'
    },
    redirect: {
      name: 'spaceControl'
    },
    component: resolve => require(['@/components/admin/index'], resolve),
    children: [
      {
        path: 'adminControl',
        name: 'adminControl',
        meta: { title: '管理' },
        redirect: {
          name: 'spaceControl'
        },
        component: resolve => require(['@/components/admin/adminControl/index'], resolve),
        children: [
          {
            path: '/adminControl/spaceControl',
            name: 'spaceControl',
            meta: { title: '空间列表' },
            component: resolve => require(['@/components/admin/adminControl/spaceControl/spaceControl'], resolve)
          },
          {
            path: '/adminControl/spaceControl/addSpace',
            name: 'spaceControl/addSpace',
            meta: { title: '添加空间' },
            hidden: true,
            component: resolve => require(['@/components/admin/adminControl/spaceControl/addSpace'], resolve)
          },
          {
            path: '/adminControl/spaceControl/spaceDetail',
            name: 'spaceControl/spaceDetail',
            meta: { title: '空间详情' },
            hidden: true,
            component: resolve => require(['@/components/admin/adminControl/spaceControl/spaceDetail'], resolve)
          },
          {
            path: '/adminControl/spaceControl/incomeDrtail',
            name: 'spaceControl/incomeDrtail',
            meta: { title: '修改空间' },
            hidden: true,
            component: resolve => require(['@/components/admin/adminControl/spaceControl/incomeDrtail'], resolve)
          },
          {
            path: '/adminControl/orderControl',
            name: 'orderControl',
            meta: { title: '订单列表' },
            component: resolve => require(['@/components/admin/adminControl/orderControl/orderControl'], resolve)
          },
          {
            path: '/adminControl/orderControl/orderDetail',
            name: 'orderControl/orderDetail',
            meta: { title: '订单详情' },
            hidden: true,
            component: resolve => require(['@/components/admin/adminControl/orderControl/orderDetail'], resolve)
          },
          {
            path: '/adminControl/distributionControl',
            name: 'distributionControl',
            meta: { title: '分销订单' },
            component: resolve => require(['@/components/admin/adminControl/distributionControl/distributionControl'], resolve)
          },
          {
            path: '/adminControl/distributionControl/distributionDetail',
            name: 'distributionControl/distributionDetail',
            meta: { title: '分销订单详情' },
            hidden: true,
            component: resolve => require(['@/components/admin/adminControl/distributionControl/distributionDetail'], resolve)
          },
          {
            path: '/adminControl/distributionControl/distributionPresale',
            name: 'distributionControl/distributionPresale',
            meta: { title: '核销' },
            hidden: true,
            component: resolve => require(['@/components/admin/adminControl/distributionControl/distributionPresale'], resolve)
          },
          {
            path: '/adminControl/voucherControl',
            name: 'voucherControl',
            meta: { title: '卡劵列表' },
            component: resolve => require(['@/components/admin/adminControl/voucherControl/voucherControl'], resolve)
          },
          {
            path: '/adminControl/voucherControl/voucherDetail',
            name: 'voucherControl/voucherDetail',
            meta: { title: '卡劵详情' },
            hidden: true,
            component: resolve => require(['@/components/admin/adminControl/voucherControl/voucherDetail'], resolve)
          },
          {
            path: '/adminControl/rechargeControl',
            name: 'rechargeControl',
            meta: { title: '充值列表' },
            component: resolve => require(['@/components/admin/adminControl/rechargeControl/rechargeControl'], resolve)
          },
          {
            path: '/adminControl/rechargeControl/rechargeDetail',
            name: 'rechargeControl/rechargeDetail',
            meta: { title: '充值详情' },
            hidden: true,
            component: resolve => require(['@/components/admin/adminControl/rechargeControl/rechargeDetail'], resolve)
          }
        ]
      }
    ]
  },
  {
    path: '*', // 所有的（*代表所有）错误页面
    component: resolve => require(['@/components/error/index'], resolve),
    hidden: true
  }
]

let router = new Router({
  routes: constantRouterMap,
  mode: 'history',
  base: '/'
})
// 异步挂载的路由
// 动态需要根据权限加载的路由表
export const asyncRouterMap = [
  {
    path: '/admin',
    // name: 'admin',
    meta: {
      title: '管理页面'
      // roles: ['admin']
    },
    redirect: {
      name: 'spaceControl'
    },
    component: resolve => require(['@/components/admin/index'], resolve),
    children: [
      {
        path: 'adminControl',
        // name: 'adminControl',
        meta: {
          title: '管理'
          // roles: ['admin']
        },
        redirect: {
          name: 'spaceControl'
        },
        component: resolve => require(['@/components/admin/adminControl/index'], resolve),
        children: [
          {
            path: '/adminControl/userControl',
            name: 'userControl',
            meta: { title: '用户列表', roles: ['admin'] },
            component: resolve => require(['@/components/admin/adminControl/userControl/userControl'], resolve)
          },
          {
            path: '/adminControl/userControl/seeAllrecord',
            name: 'userControl/seeAllrecord',
            meta: { title: '用户消费详情', roles: ['admin'] },
            hidden: true,
            component: resolve => require(['@/components/admin/adminControl/userControl/seeAllrecord'], resolve)
          },
          {
            path: '/adminControl/applicationAdmission',
            name: 'applicationAdmission',
            meta: { title: '入驻申请', roles: ['admin'] },
            component: resolve => require(['@/components/admin/adminControl/applicationAdmission/applicationAdmission'], resolve)
          },
          {
            path: '/adminControl/activityControl',
            name: 'activityControl',
            meta: { title: '分销活动', roles: ['admin'] },
            component: resolve => require(['@/components/admin/adminControl/activityControl/activityControl'], resolve)
          },
          {
            path: '/adminControl/activityControl/addActivity',
            name: 'activityControl/addActivity',
            hidden: true,
            meta: { title: '添加活动', roles: ['admin'] },
            component: resolve => require(['@/components/admin/adminControl/activityControl/addActivity'], resolve)
          },
          {
            path: '/adminControl/activityControl/activityDetail',
            name: 'activityControl/activityDetail',
            hidden: true,
            meta: { title: '活动详情', roles: ['admin'] },
            component: resolve => require(['@/components/admin/adminControl/activityControl/activityDetail'], resolve)
          },
          {
            path: '/adminControl/activityControl/editActivity',
            name: 'activityControl/editActivity',
            hidden: true,
            meta: { title: '修改活动', roles: ['admin'] },
            component: resolve => require(['@/components/admin/adminControl/activityControl/editActivity'], resolve)
          },
          {
            // path: '/setting/permissionSetting',
            path: '/adminControl/permissionSetting',
            name: 'permissionSetting',
            meta: { title: '权限设置', roles: ['admin'] },
            component: resolve => require(['@/components/admin/setting/permissionSetting/permissionSetting'], resolve)
          }
        ]
      },
      {
        path: '/statisticalAnalysis',
        name: 'statisticalAnalysis',
        // component: Layout,
        meta: {
          title: '统计分析',
          // icon: 'el-icon-setting',
          roles: ['admin']
        },
        redirect: {
          name: 'userAnalysis'
        },
        component: resolve => require(['@/components/admin/statisticalAnalysis/index'], resolve),
        children: [
          {
            name: 'userAnalysis',
            path: '/statisticalAnalysis/userAnalysis',
            meta: {
              title: '用户分析', icon: 'el-icon-menu', roles: ['admin']
            },
            component: resolve => require(['@/components/admin/statisticalAnalysis/orderAnalysis/orderAnalysis'], resolve)
          },
          {
            path: '/statisticalAnalysis/orderAnalysis',
            name: 'orderAnalysis',
            meta: { title: '订单分析', roles: ['admin'] },
            component: resolve => require(['@/components/admin/statisticalAnalysis/orderAnalysis/orderAnalysis'], resolve)
          },
          {
            path: '/statisticalAnalysis/spaceAnalysis',
            name: 'spaceAnalysis',
            meta: { title: '空间分析', roles: ['admin'] },
            component: resolve => require(['@/components/admin/statisticalAnalysis/spaceAnalysis/spaceAnalysis'], resolve)
          }
        ]
      }
      // {
      //   path: 'setting',
      //   name: 'setting',
      //   meta: { title: '设置', roles: ['admin'] },
      //   component: resolve => require(['@/components/admin/setting/index'], resolve),
      //   // component: Layout,
      //   redirect: {
      //     name: 'permissionSetting'
      //   },
      //   children: [
      //     {
      //       path: '/setting/permissionSetting',
      //       // path: '/adminControl/permissionSetting',
      //       name: 'permissionSetting',
      //       meta: { title: '权限设置', roles: ['admin'] },
      //       component: resolve => require(['@/components/admin/setting/permissionSetting/permissionSetting'], resolve)
      //     }
      //   ]
      // }
    ]
  },
  // { path: '*', redirect: '/error', hidden: true },
  {
    path: '*',
    name: 'error',
    meta: { title: 'Error' },
    redirect: {
      name: 'noResources'
    },
    component: resolve => require(['@/components/error/index'], resolve),
    children: [
      {
        path: 'noControl',
        name: 'noControl',
        meta: { title: 'ERROR' },
        component: resolve => require(['@/components/error/noControl/noControl'], resolve)
      },
      {
        path: 'noResources',
        name: 'noResources',
        meta: { title: '没有找到资源' },
        component: resolve => require(['@/components/error/noResources/noResources'], resolve)
      }
    ]
  }
]
// router.beforeEach((to, from, next) => {
//   console.log(this.$router, to.path)
//   if (to.matched.length === 0) {
//     from.name ? next({ name: from.name }) : next('/error')
//   } else {
//     if (to.meta.isNoCheck) {
//       next()
//     } else {
//       next()
//       if (store.state.userInfo.length > 0) {
//         next()
//       } else {
//         // if (to.name !== 'login') {
//         //   next({ name: 'user' })
//         //   this.$message.error('你还未登陆')
//         // }
//       }
//     }
//   }
// })
// router.beforeEach((to, from, next) => {
//   console.log(this.$router, to.path)
//   // console.log(gloablData)
//   if (to.path !== '/user/login') {
//     let _token = store.state.userInfo.token
//     console.log(store, _token, 'store')
//     if (_token && _token.length > 0) {
//       if (to.matched.length === 0) {
//         from.name ? next({ name: from.name }) : next('/error')
//       } else {
//         if (to.meta.isNoCheck) {
//           next()
//         } else {
//           next()
//           if (store.state.userInfo.length > 0) {
//             next()
//           } else {
//             // if (to.name !== 'login') {
//             //   next({ name: 'user' })
//             //   this.$message.error('你还未登陆')
//             // }
//           }
//         }
//       }
//       // next()
//     } else {
//       let _loToken = window.localStorage.getItem('__userInfo__')
//       console.log(_loToken)
//       if (_loToken) {
//         next()
//       } else {
//         next({ path: '/user/login' })
//       }
//     }
//   } else {
//     next()
//   }
// })
// router.afterEach((to, from, next) => {
//   let routerTitle = ''
//   if (to.meta.title) {
//     routerTitle = `${to.meta.title}`
//   } else {
//     routerTitle = 'fangcun'
//   }
//   document.title = routerTitle
// })
router.onError((error) => {
  const pattern = /Loading chunk (\d)+ failed/g
  const isChunkLoadFailed = error.message.match(pattern)
  const targetPath = router.history.pending.fullPath
  if (isChunkLoadFailed) {
    router.replace(targetPath)
  }
})
export default router
